package com.markerhub.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.markerhub.common.lang.Result;
import com.markerhub.entity.Blog;
import com.markerhub.service.BlogService;
import com.markerhub.util.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.security.PublicKey;
import java.time.LocalDateTime;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zxc
 * @since 2021-06-11
 */
@RestController

public class BlogController {
    @Autowired
    BlogService blogService;


    @GetMapping("/blog")
    public Result list(@RequestParam (defaultValue = "1") Integer currentPage){
        Page page = new Page(currentPage,5);
        IPage pageData = blogService.page(page, new QueryWrapper<Blog>().orderByDesc("created"));

        return  Result.success(pageData );
    }
    @GetMapping("/blog/{id}")
    public Result detail(@PathVariable(name ="id") Long id){
        Blog blog = blogService.getById(id);
        //判断是否为空
        Assert.notNull(blog,"该博客已被删除");



        return  Result.success(blog);
    }


    @RequiresAuthentication
    @PostMapping("/blog/edit")
    public Result edit(@Validated @RequestBody Blog blog){
        Blog temp =null;
        if (blog.getId()  != null)
        {
            temp=blogService.getById(blog.getId());
            //只能编辑自己的文章
            Assert.isTrue(temp.getId().longValue() == ShiroUtils.getProfile().getId().longValue(),"没有权限编辑");

        }else {
            temp = new Blog();
            temp.setId(ShiroUtils.getProfile().getId());
            temp.setCreated(LocalDateTime.now());
            temp.setStatus(0);

        }
        BeanUtils.copyProperties(blog,temp,"id","userId","created","status");
        blogService.saveOrUpdate(temp);

        return  Result.success(null);
    }


}
